package com.ziytek.web.citizen.service;

import com.ziytek.web.citizen.config.ConfigInit;
import com.ziytek.web.citizen.config.caffeine.CaffeineUtil;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.mapper.OperationDetailMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 恒流板通讯异常服务
 *
 * @author fenghx
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class BoardAbnormalService {
    private final ConfigInit configInit;
    private final OperationDetailMapper operationDetailMapper;
    private final InterruptIssuedService interruptIssuedService;

    public void boardAbnormal(Integer cabinetNo, Integer boardNo, Integer channelNo, Integer current, Integer voltage) {
        Assert.notNull(boardNo, "恒流板通讯异常处理boardAbnormal boardNo is null");
        Assert.notNull(current, "恒流板通讯异常处理boardAbnormal current is null");
        Assert.notNull(voltage, "恒流板通讯异常处理boardAbnormal voltage is null");
        Assert.notNull(channelNo, "恒流板通讯异常处理boardAbnormal channelNo is null");
        Assert.notNull(cabinetNo, "恒流板通讯异常处理boardAbnormal cabinetNo is null");

        // 判断电池电芯电压及电流是否全部为0,是则为空闲电池,一块恒流板8个通道电池电压及电流全为0则认为是通讯异常
        boolean leZero = current <= NumConstant.INT_ZERO && voltage <= NumConstant.INT_ZERO;
        var key = CaffeineUtil.BOARD_ABNORMAL_KEY + cabinetNo + CaffeineUtil.CACHE_KEY_SYMBOL + boardNo;
        Boolean bn = CaffeineUtil.get(key);
        if (Boolean.TRUE.equals(bn)) {
            if (leZero) {
                return;
            }

            // 电流或电压大于0，则删除该电池的通讯异常缓存
            CaffeineUtil.delete(key);
            CaffeineUtil.delete(CaffeineUtil.CHANNEL_ABNORMAL_KEY_ + key + CaffeineUtil.CACHE_KEY_SYMBOL + channelNo);
        }

        if (leZero) {
            var keyChanel = CaffeineUtil.CHANNEL_ABNORMAL_KEY_ + key + CaffeineUtil.CACHE_KEY_SYMBOL + channelNo;
            CaffeineUtil.set(keyChanel, NumConstant.INT_ONE, 30);
        }

        Map<String, CaffeineUtil.CacheObject<?>> map = CaffeineUtil.getAllStartWith(CaffeineUtil.CHANNEL_ABNORMAL_KEY_ + key);
        if (CollectionUtils.isEmpty(map)) {
            return;
        }

        List<Integer> intList = map.values().stream()
                .filter(v -> v instanceof CaffeineUtil.CacheObject<?> cacheObject
                        && cacheObject.data instanceof Integer i)
                .map(v -> (Integer) v.data)
                .toList();

        int count = intList.stream().filter(i -> i == NumConstant.INT_ONE).toList().size();

        // 总数为8,则认为是通讯异常
        if (NumConstant.INT_EIGHT == count) {
            //log.info("机柜[{}]恒流板[{}]数据通讯异常", cabinetNo, boardNo);
            CaffeineUtil.set(key, Boolean.TRUE, 24 * 60 * 60);

            // 配置未开启
            if (!configInit.isInterruptAutomatic()) {
                return;
            }

            // 中断操作
            var cab = String.valueOf(channelNo);
            // 一个恒流版全部8个通道全部进行中断操作
            for (int i = NumConstant.INT_ONE; i <= NumConstant.INT_EIGHT; i++) {
                var op = operationDetailMapper.findOneDetailByCellNum(cab, boardNo, i);
                if (null == op) {
                    continue;
                }

                // 通道操作、定时任务机缓存中断
                interruptIssuedService.interruptIssuedByOperationDetail(op, null);
            }
        }
    }
}
